package net.sk.china.front.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sk.china.common.model.user.UserToken;
import net.sk.china.front.dao.user.UserTokenDao;
import net.sk.china.front.service.user.UserTokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * UserTokenServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/4 16:03
 * // +----------------------------------------------------------------------
 */

@Service
public class UserTokenServiceImpl extends ServiceImpl<UserTokenDao, UserToken> implements UserTokenService {

    @Resource
    private UserTokenDao dao;

    /**
     * 绑定token关系
     * @param openId openid
     * @param type 类型
     * @param userId 用户ID
     */
    @Override
    public void bind(String openId, int type, Integer userId) {
        UserToken userToken = new UserToken();
        userToken.setToken(openId);
        userToken.setType(type);
        userToken.setUid(userId);

        dao.insert(userToken);
    }

    /**
     * 获取UserToken
     * @param token openid
     * @param type 类型
     * @return {@code UserToken}
     */
    @Override
    public UserToken getByOpenidAndType(String token, int type) {
        LambdaQueryWrapper<UserToken> query= new LambdaQueryWrapper<>();
        query.eq(UserToken::getType, type).eq(UserToken::getToken, token);
        query.last("limit 1");

        return dao.selectOne(query);
    }

    /**
     * 查询用户TOKEN信息
     * @param userId 用户ID
     * @param type 类型
     * @return {@code UserToken}
     */
    @Override
    public UserToken getTokenByUserId(Integer userId, int type) {
        LambdaQueryWrapper<UserToken> query = new LambdaQueryWrapper<>();
        query.eq(UserToken::getUid, userId).eq(UserToken::getType, type);
        query.last("limit 1");

        return dao.selectOne(query);
    }
}
